package com.news.servlet;


import com.alibaba.fastjson.JSON;
import com.news.pojo.Comments;
import com.news.service.CommentsService;
import com.news.service.impl.CommentsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Servlet的创建步骤：
 * 1.创建servlet类，继承HttpServlet
 * 2.重写doxxx方法  doGet获取后端的数据  doPost:获取前端的大量数据（文件上传）
 * 3.配置wen.xml.将servlet的访问URL进行匹配
 *          1.一个servlet可以由多个URL匹配    opr进行控制 opr=add/getAll
 *          2.一个URL只能有一个servlet匹配
 *
 */
@WebServlet("/comments.do")
public class CommentsServlet extends HttpServlet {
    private static final long serialVersionUID =1L;
    private CommentsService commentsService =new CommentsServiceImpl();



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            //2.设置响应内容格式
            response.setContentType("text/html;charset=UTF-8");
            //3.控制请求的逻辑
            String opr = request.getParameter("opr");
            switch (opr){
                case "add":{
                    add(request,response);
                    break;
                }
                case "getAll":{
                    getAll(request,response);
                    break;
                }
                case "getById":{
                    getById(request,response);
                    break;
                }

                default:{
                    response.sendRedirect("/404.jsp");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("/500.jsp");
        }
    }

    //我是大帅哥

    /**
     * 新增评论
     * @param
     * @param
     * @throws ServletException
     * @throws IOException
     */
    public void  add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //service处理页面
        System.out.println("进入评论add");
        String ccontent = request.getParameter("cconent");
        String cauthor = request.getParameter("cauthor");
        String cip =request.getParameter("cip");

        int cnid = Integer.valueOf(request.getParameter("cnid"));


        Comments comments = new Comments();
        comments.setCcontent(ccontent);
        comments.setCauthor(cauthor);
        comments.setCip(cip);
        comments.setCnid(cnid);
        //session.getAttribute("commments_add.jsp",commentsService.addComments(comments));
        //响应
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out =response.getWriter();
        out.print(JSON.toJSONString(commentsService.addComments(comments)));
        out.flush();
        out.close();
    }

    /**
     * 查询所有评论
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
            //响应
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(commentsService.getAll()));
        out.flush();
        out.close();
    }
    /**
     * 通过新闻id查询评论
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void getById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //响应
        System.out.println("进入评论getById");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        int  tid=Integer.parseInt(request.getParameter("tid")) ;
        out.print(JSON.toJSONString(commentsService.getAllByNid(tid)));
        out.flush();
        out.close();
    }


}
